package com.cn.algorithm.two.line;

import java.util.Arrays;

public class QuickSort {

    public static void main(String[] args) {
        int[] arr = {9, 7, 4, 2, 5, 3, 8};
        sort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr, int start, int end) {
        if (arr == null || arr.length <= 1 || start >= end) {
            return;
        }
        int left = start;
        int right = end;
        int tmp = arr[left];
        while (left < right) {
            while (left < right && arr[left] < tmp) {
                left++;
            }
            while (left < right && arr[right] > tmp) {
                right--;
            }
            swap(arr, left, right);
        }
        sort(arr, start, left - 1);
        sort(arr, left + 1, right);
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
